package com.health.system.service.organiza.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import com.health.system.model.organiza.DataOrganizations;

import com.health.system.model.system.User;
import com.health.system.model.system.UserRole;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.health.system.service.organiza.DataOrganizationsManager;
import com.health.system.service.system.UserManager;
import com.health.system.service.system.UserRoleManager;
import com.health.system.springSecurity.UserInfo;
import com.health.system.dao.organiza.DataOrganizationsDao;
import com.health.system.javacommon.base.BaseManager;
import com.health.system.javacommon.base.EntityDao;
import com.health.system.springSecurity.LogonUser;
import com.health.system.javacommon.util.RandomNum;

/**
* Title: 组织机构 业务处理层 
* Description: spring事务实现层,调用Dao层取数据.健康通 
* Copyright: Copyright (c) 2011-03 
* Company:  
* 
* @author you name 
* @version 1.0 
*/

@Component("dataOrganizationsManager")
@Transactional
public class DataOrganizationsManagerImpl extends BaseManager<DataOrganizations,java.lang.String> 
					implements DataOrganizationsManager {
	
	/**定义Dao*/					
	private DataOrganizationsDao dataOrganizationsDao;
	private UserManager userManager;
	private UserRoleManager userRoleManager;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性*/
	
	public void setUserManager(UserManager manager) {
		this.userManager = manager;
	}

	public void setUserRoleManager(UserRoleManager userRoleManager) {
		this.userRoleManager = userRoleManager;
	}

	public void setDataOrganizationsDao(DataOrganizationsDao dao) {
		this.dataOrganizationsDao = dao;
	}
	
	/**获取Dao*/	
	public EntityDao getEntityDao() {
		return this.dataOrganizationsDao;
	}
	
	/**
	* <p>分页查询DataOrganizations<br>
	* @param PageRequest pr
	* @return Page
	* @author Kingter
	*/
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		return dataOrganizationsDao.findByPageRequest(pr);
	}
	
	/**
	* <p>批量删除 组织机构<br>
	* @param DataOrganizations u
	* @return Boolean
	* @author Kingter
	*/
	public Boolean  removeByIdArry(DataOrganizations u) {
		Boolean result =false;
		//定义实现删除数据条数
		int i =-1;
		//要删除的数据量
		int size=u.getIdList().size();
		i =dataOrganizationsDao.removeByIdArry(u);
		//判断要删除的量是否与实现删除量相等,否则抛出异常,spring将全自动事务回滚
		if(i==size){
			result =true;
		}else{
			throw new RuntimeException();
		}
		return result;
	}
	
	
	public List userDataOrganizationsTree(Map map){
		return dataOrganizationsDao.userDataOrganizationsTree(map);
	}
	/**
	 * <p>新增机构</p>
	 * <p>组织机构数据权限表一条数据.写群组表</p>
	 */
	public String saveInfo(DataOrganizations u){
		try{
			/**生成主键ID*/
			String id = "9";
			u.setId(id);
					 
			return id;
		}catch(Exception exe){
			throw new RuntimeException();
		}
	}
	 
	/**
	 * 操作员 修改 个人机构信息
	 * @param u DataOrganizations
	 */
	public void personal(DataOrganizations u){ 
		super.update(u);
	}

	
	/**
	* <p>开通机构操作</p>
	* <p>组织机构一条数据,操作员表一条数据,写入角色和操作关联</p>
	* @param DataOrganizations u
	* @author bzq
	*/
	public void dredge(DataOrganizations u){
		try{
			//从登录用户类中取当前用户信息
			UserInfo userInfo = LogonUser.logonUser();
			/**操作员的类型-1-SI管理系统,2-SI自服系统,3-EC自服务系统,9-其它*/
			String userNature = "9";
			if(null!=userInfo){
				userNature = userInfo.getUserNature();
			}
			/**组织机构一条数据*/
			String organizations_id_ = getId();
			//System.out.println("u.getHigherId() = "+u.getHigherId() +",userInfo.getCpId() = "+userInfo.getCpId());
			if(null!=u.getHigherId()&&u.getHigherId().length()>0){
				 
			}else{
				/**默认操作所在机构为开通机构的上级机构*/
				if(null!=userInfo){
				  u.setHigherId(userInfo.getCpId());
				}
			}
			u.setId(organizations_id_);
			User user_one = u.getUser();
			u.setCpCode(RandomNum.getLowerCaseLetters(6));
			u.setContrNo("1065"+RandomNum.password(4));
			if(null!=userInfo){
			   u.setOpenPeople(userInfo.getUserId());
			}
			/**保存:组织机构信息*/
			super.save(u);

			
			/**设置机构ID*/
			user_one.setCompany(organizations_id_);
			user_one.setCpCode(u.getCpCode());
			/**保存:操作员表一条数据*/
			if(null!=userManager){
			    userManager.save(user_one);
		    }else{
				System.out.println("系统配置异常,userManager为空");
			}

			
			/**写入角色和操作关联*/
			UserRole userRole = u.getUserRole();
			String[] roleIdArray = userRole.getRoleIdArray();
			System.out.println("user.Id="+user_one.getId());
			String[] testString = userRole.getRoleIdStr().split(",");
			System.out.println("testString length = "+testString.length);
			for(int mm=0;null!=testString&&mm<testString.length;mm++){
				if(testString[mm].trim().length()>0){
					userRole.setUserId(user_one.getId());
					userRole.setRoleId(testString[mm]);
					if(null!=userRoleManager){
					    userRoleManager.save(userRole);
					}else{
						System.out.println("系统配置异常,userRoleManager为空");
					}
				}
			}
		}catch(Exception exe){
			exe.printStackTrace();
			throw new RuntimeException();
		}
	}
	
	
}
